#!/usr/bin/python3
# -*- coding: utf-8 -*-

import allure
from playwright.sync_api import Page

from models.base_page import BasePage
from models.network_page.router_page.create_router_page import CreateRouterPage
from models.network_page.router_page.router_details_page import \
    RouterDetailPage
from utils.log import log


class RouterPage(BasePage):
    def __init__(self, page: Page):
        super().__init__(page)
        self.route = "routers"
        self.create_page = CreateRouterPage(page)
        self.detail_page = RouterDetailPage(page)

    @allure.step("点击创建路由器按钮")
    def click_create_router_button(self) -> None:
        self.toolbar.click_create_router_button()
        self.wait_for_timeout(5000)
        self.expect.to_have_url(self.create_page.route)
        self.create_page.check_page_text_context()

    @allure.step("点击更多操作按钮")
    def click_more_operation_button_by_keyword(self, router_id_keyword: str) -> None:
        """点击更多操作按钮,通过router_id关键字"""
        self.page.locator(f"//a[@data-value='{router_id_keyword}']/parent::span/following-sibling::div").click()

    @allure.step("点击更多操作按钮")
    def click_more_operation_button_by_role_heading(self, router_name: str, router_id_keyword: str) -> None:
        """点击更多操作按钮,通过router_id关键字"""
        self.page.get_by_role("heading", name=f"名称: {router_name} (ID: {router_id_keyword}").get_by_role("textbox").\
            click()

    @allure.step("点击修改按钮")
    def click_modify_button(self):
        """点击修改按钮"""
        self.page.locator("a").filter(has_text="修改").first.click()

    @allure.step("点击删除按钮")
    def click_delete_button(self):
        """点击删除按钮"""
        self.page.locator("a").filter(has_text="删除").nth(2).click()

    @allure.step("点击扩容按钮")
    def click_resize_button(self):
        """点击扩容按钮"""
        self.page.locator("a").filter(has_text="扩容").click()

    @allure.step("点击启用网络健康探测按钮")
    def click_enable_network_health_detection_button(self):
        """点击启用网络健康探测按钮"""
        self.page.locator("a").filter(has_text="启用网络健康探测").click()

    @allure.step("点击重启按钮")
    def click_restart_button(self):
        """点击重启按钮"""
        self.page.locator("a").filter(has_text="重启").click()

    @allure.step("点击启用IPv6按钮")
    def click_enable_ipv6_button(self):
        """点击启用IPv6按钮"""
        self.page.locator("a").filter(has_text="启用 IPv6").click()

    @allure.step("点击关闭IPv6按钮")
    def click_close_ipv6_button(self):
        """点击关闭IPv6按钮"""
        self.page.locator("a").filter(has_text="关闭 IPv6").click()

    @allure.step("点击加入到项目按钮")
    def click_join_to_project_button(self):
        """点击加入到项目按钮"""
        self.page.locator("a").filter(has_text="添加到项目").first.click()

    @allure.step("点击从项目移除按钮")
    def click_remove_from_project_button(self):
        """点击从项目移除按钮"""
        self.page.locator("a").filter(has_text="从项目中移除").nth(1).click()

    def get_delete_router_alert_info_text_content(self):
        """弹框组件-获取弹框内容文本"""
        if self.page.locator("#modalId").locator(".alert-info").is_visible():
            alert_text_content = (
                self.page.locator("#modalId").locator(".alert-info").text_content()
            )
            log.info(f"弹框文本:{alert_text_content}")
            return alert_text_content

    def check_router_page_user_guide_element_to_be_visible(self):
        """检查使用指南元素是否可见"""
        with allure.step("检查RouterPage:使用指南元素是否可见"):
            self.expect.to_be_visible(self.page.locator(".intro"))  # 使用指南栏目
            self.expect.to_be_visible(self.page.get_by_text("使用指南"))
            self.expect.to_be_visible(self.page.locator("li").filter(has_text="VPC 网络").get_by_role("link"))
            self.expect.to_be_visible(self.page.get_by_text("收起"))
            self.expect.to_be_visible(self.page.locator(".svg-icon > use"))
            self.expect.to_contain_text(self.page.locator(".tab-intro .text"),
                                        "专属私有网络 VPC 帮助您在公有云上构建出一个专属隔离的网络环境。")

    def check_router_page_table_element_to_be_visible(self):
        """检查工具栏及table是否可见"""
        with allure.step("检查RouterPage:工具栏及table是否可见"):
            self.expect.to_be_visible(self.page.locator(".vpc-toolbar"))
            self.expect.to_be_visible(self.page.get_by_role("link", name="G", exact=True))
            self.expect.to_be_visible(self.page.locator("a").filter(has_text="创建 VPC 网络"))
            self.expect.to_be_visible(self.page.locator("select[name=\"search_key\"]"))
            self.expect.to_be_visible(self.page.get_by_placeholder(" "))
            self.expect.to_be_visible(self.page.locator("select[name=\"limit\"]"))
